<?php
	require_once 'base.dao.php';
	require_once 'sections_vao.php';
	
	class Section_manager extends Base{
		
		public function __construct($conn){
			parent::__construct($conn);		
		}
		
		public function getSectionsValues(){
			$sql = sprintf("select * from SECTIONS where active = %d order by order_section",
						   1);
			
			$result = mysql_query($sql,$this->conn);
	    	
	    	if(!$result){
	    		throw new Exception('It could not proceed your request to the Data Base.');
	    	}
	    	
	    	$return = array();
	    	
	    	while($row = mysql_fetch_array($result)){
	    		extract($row);
	    		$section = new Section($ID_SECTION,
	    							   $NAME_SECTION,
	    							   $CONTENT,
	    							   $TEXT,
	    							   $URL,
	    							   $ACTIVE,
	    							   $ORDER_SECTION,
	    							   $PAGE_TITLE);
	    		$return[] = $section;
	    	}
	    		    	
	    	return $return;
		}
		
		public function getSectionsForAdministration(){
			$sql = "select * from SECTIONS";
			
			$result = mysql_query($sql,$this->conn);
	    	
	    	if(!$result){
	    		throw new Exception('It could not proceed your request to the Data Base.');
	    	}
	    	
	    	$return = array();
	    	
	    	while($row = mysql_fetch_array($result)){
	    		extract($row);
	    		$section = new Section($ID_SECTION,
	    							   $NAME_SECTION,
	    							   $CONTENT,
	    							   $TEXT,
	    							   $URL,
	    							   $ACTIVE,
	    							   $ORDER_SECTION,
	    							   $PAGE_TITLE);
	    		$return[] = $section;
	    	}
	    		    	
	    	return $return;
		}
		
		public function getSection($id_section){
			$sql = sprintf("select * from SECTIONS where id_section = %d",
						   $id_section);
			
			$result = mysql_query($sql,$this->conn);
	    	
	    	if(!$result || (mysql_num_rows($result) <= 0)){
	    		throw new Exception('The section that you are trying to edit does not exist.');
	    	}
	    	
	    	if($row = mysql_fetch_array($result)){
	    		extract($row);
	    		$section = new Section($ID_SECTION,
	    							   $NAME_SECTION,
	    							   $CONTENT,
	    							   $TEXT,
	    							   $URL,
	    							   $ACTIVE,
	    							   $ORDER_SECTION,
	    							   $PAGE_TITLE);
	    		return $section;
	    	}
	    		    	
		}
		
		public function update($section){
			$sql = sprintf("update SECTIONS
							set name_section = '%s', content = '%s', url = '%s', active = %d, page_title = '%s'
							where id_section = %d",
							$section->getNameSection(),
							$section->getContent(),
							$section->getUrl(),
							$section->getActive(),
							$section->getPageTitle(),
							$section->getIdSection());
										
			$result = mysql_query($sql,$this->conn);
			
			if(!$result){
	    		throw new Exception('The section that you are trying to edit does not exist.');
	    	}
		}
		
	}
?>